package com.ruoyi.ihome.service.impl;

import java.util.*;

import com.ruoyi.ihome.domain.IhHome;
import com.ruoyi.ihome.domain.IhTopic;
import com.ruoyi.ihome.domain.IhTopicTag;
import com.ruoyi.ihome.domain.SearchCriteria;
import com.ruoyi.ihome.mapper.IhTopicMapper;
import com.ruoyi.ihome.mapper.IhTopicTagMapper;
import com.ruoyi.ihome.service.IIhTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 帖子管理Service业务层处理
 *
 * @author me
 * @date 2024-08-13
 */
@Service
public class IhTopicServiceImpl implements IIhTopicService {
    @Autowired
    private IhTopicMapper ihTopicMapper;

    @Autowired
    private IhTopicTagMapper ihTopicTagMapper;

    /**
     * 查询帖子管理
     *
     * @param id 帖子管理主键
     * @return 帖子管理
     */
    @Override
    public IhTopic selectIhTopicById(Long id) {
        return ihTopicMapper.selectIhTopicById(id);
    }

    /**
     * 查询房子信息列表
     *
     * @return 房子信息集合
     */
    @Override
    public List<IhHome> gethouse() {
        List<IhHome> houses = ihTopicMapper.gethouse();
        return houses;
    }

    /**
     * 查询合租帖子列表
     *
     * @return 合租帖子集合
     */
    @Override
    public List<IhTopic> selectSharedFlats(Integer userId) {
        List<IhTopic> ihTopics = ihTopicMapper.selectSharedFlats(userId);
        return ihTopics;
    }

    @Override
    public int insertTopicAndUser(IhTopic ihTopic) {
        int res = ihTopicMapper.insertTopicAndUser(ihTopic);
        List<IhTopicTag> ihTopicTags = ihTopicTagMapper.selectAllIhTopicTag();
        System.out.println("ihTopicTags" + ihTopicTags);
        String title = ihTopic.getTitle();
        String content = ihTopic.getContent();
        Iterator<IhTopicTag> it = ihTopicTags.iterator();
        while (it.hasNext()) {
            IhTopicTag ihTopicTag = it.next();
            String tagName = ihTopicTag.getTagName();
            int res1 = title.indexOf(tagName);
            int res2 = content.indexOf(tagName);
            if (res1 >= 0 | res2 >= 0) {
                Map map = new HashMap();
                map.put("topicId", ihTopic.getId());
                map.put("tagId", ihTopicTag.getId());
                int resu = ihTopicTagMapper.insertTopicAndTag(map);
            }
        }
        return res;
    }

    /**
     * 查询求租帖子列表
     *
     * @return 求租帖子集合
     */
    @Override
    public List<IhTopic> selectAskingForRent(Integer userId) {
        List<IhTopic> ihTopics = ihTopicMapper.selectAskingForRent(userId);
        return ihTopics;
    }

    /**
     * 查询合租帖详细信息
     *
     * @return 合租帖子详细信息
     */
    @Override
    public IhTopic selectSharedFlatsTopicDetails(Integer topicId,Integer userId) {
        Map map = new HashMap<>();
        map.put("topicId",topicId);
        map.put("userId",userId);
        IhTopic ihTopic = ihTopicMapper.selectSharedFlatsTopicDetails(map);
        return ihTopic;
    }

    /**
     * 查询求租帖详细信息
     *
     * @return 求租帖子详细信息
     */
    @Override
    public IhTopic selectAskingForRentTopicDetails(Integer topicId,Integer userId) {
        Map map = new HashMap<>();
        map.put("topicId",topicId);
        map.put("userId",userId);
        IhTopic ihTopic = ihTopicMapper.selectAskingForRentTopicDetails(map);
        return ihTopic;
    }

    /**
     * 查询帖子管理列表
     *
     * @param ihTopic 帖子管理
     * @return 帖子管理
     */
    @Override
    public List<IhTopic> selectIhTopicList(IhTopic ihTopic) {
        return ihTopicMapper.selectIhTopicList(ihTopic);
    }

    @Override
    public List<IhTopic> selectAllTopic(Integer userId,String serchValue) {
        List<IhTopic> ihTopics = ihTopicMapper.selectAllTopic(userId);
        System.out.println(serchValue);
        if (serchValue != null){
            Iterator<IhTopic> it = ihTopics.iterator();
            while (it.hasNext()){
                IhTopic ihTopic = it.next();
                String title = ihTopic.getTitle();
                String content = ihTopic.getContent();
                String userName = ihTopic.getUser().getUserName();
                String nickName = ihTopic.getUser().getNickName();
                int res1 = title.indexOf(serchValue);
                int res2 = content.indexOf(serchValue);
                int res3 = userName.indexOf(serchValue);
                int res4 = nickName.indexOf(serchValue);
                if (res1 == -1 && res2 == -1 && res3 == -1 && res4 == -1){
                    it.remove();
                }
            }
        }
        return ihTopics;
    }

    /**
     * 新增帖子管理
     *
     * @param ihTopic 帖子管理
     * @return 结果
     */
    @Override
    public int insertIhTopic(IhTopic ihTopic) {
        return ihTopicMapper.insertIhTopic(ihTopic);
    }

    /**
     * 修改帖子管理
     *
     * @param ihTopic 帖子管理
     * @return 结果
     */
    @Override
    public int updateIhTopic(IhTopic ihTopic) {
        return ihTopicMapper.updateIhTopic(ihTopic);
    }

    /**
     * 批量删除帖子管理
     *
     * @param ids 需要删除的帖子管理主键
     * @return 结果
     */
    @Override
    public int deleteIhTopicByIds(Long[] ids) {
        return ihTopicMapper.deleteIhTopicByIds(ids);
    }

    /**
     * 删除帖子管理信息
     *
     * @param id 帖子管理主键
     * @return 结果
     */
    @Override
    public int deleteIhTopicById(Long id) {
        return ihTopicMapper.deleteIhTopicById(id);
    }

    @Override
    public int attentionTopic(Map map) {
        int res = ihTopicMapper.attentionTopic(map);
        return res;
    }

    @Override
    public int cancelAttention(Map map) {
        int res = ihTopicMapper.cancelAttention(map);
        return res;
    }
}
